﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GraphDrawer.GraphProvider;

namespace GraphDrawer {
	public partial class WizardMenu : UserControl {
		public static readonly GraphProviderServiceSoapClient graphProvider = new GraphProviderServiceSoapClient();
		public static EmptyPopUp PopUp = new EmptyPopUp();
		
		private Dictionary<String, SolidColorBrush> statesColors = new Dictionary<string, SolidColorBrush>();

		private Entry entry = new Entry();
		private Source networkSource = new Source();
		private DiseaseSelector diseaseSource = new DiseaseSelector();
		private SetupSimulation setup;
		private Simulation simulation;
		private SimulationReportChart chart = new SimulationReportChart();
		
		public WizardMenu() {
			InitializeComponent();

			wizard.Pages.Add(entry);
			wizard.Pages.Add(networkSource);
			wizard.Pages.Add(diseaseSource);
			setup = new SetupSimulation(this);
			wizard.Pages.Add(setup);
			simulation = new Simulation(this);
			wizard.Pages.Add(simulation);
			wizard.Pages.Add(chart);

			PopUp.Visibility = Visibility.Collapsed;
			LayoutRoot.Children.Add(PopUp);

			networkSource.isSelectedSource += new RoutedEventHandler(networkSource_isSelectedSource);
			diseaseSource.isSelectedDisease += new RoutedEventHandler(diseaseSource_isSelectedDisease);
			SizeChanged += new SizeChangedEventHandler(WizardMenu_SizeChanged);
			wizard.PageEvent += new WizardPageEvent(WizardMenu_PageEvent);
		}

		public void EnableNext() {
			wizard.btNext.IsEnabled = true;
		}

		public void DisableNext() {
			wizard.btNext.IsEnabled = false;
		}
		
		public SetupSimulation GetSetup() {
			return setup;
		}
		
		public void GetSetupSimulation() {
			if (networkSource.sourceMethod == typeof(ImportNetwork)) {
				setup.SimulationSetup.diseaseModelGraphML = diseaseSource.GraphML;
				setup.SimulationSetup.networkGraphML = networkSource.import.graphML;
			}
			else if (networkSource.sourceMethod == typeof(GenerateNetwork)) {
				setup.SimulationSetup.diseaseModelGraphML = diseaseSource.GraphML;
				//TODO: check and fix
				setup.SimulationSetup.networkGraph = networkSource.generator.graph;
			}
		}

		public SolidColorBrush GetColorForDisease(String Disease) {
			if (statesColors.ContainsKey(Disease))
				return statesColors[Disease];
			else
				return new SolidColorBrush(Colors.Transparent);
		}

		public String[] GetAvailableStates() {
			return statesColors.Keys.ToArray();
		}

		private void fullScreen_Click(object sender, RoutedEventArgs e) {
			//TODO: add changing style App.Current.Resources
			App.Current.Host.Content.IsFullScreen = !App.Current.Host.Content.IsFullScreen;
		}

		private Color HexColor(String hex) {
			//remove the # at the front
			hex = hex.Replace("#", "");

			byte a = 255;
			byte r = 255;
			byte g = 255;
			byte b = 255;

			int start = 0;

			//handle ARGB strings (8 characters long)
			if (hex.Length == 8) {
				a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
				start = 2;
			}

			//convert RGB characters to bytes
			r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
			g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
			b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);

			return Color.FromArgb(a, r, g, b);
		}

		private void networkSource_isSelectedSource(object sender, RoutedEventArgs e) {
			EnableNext();
		}

		private void diseaseSource_isSelectedDisease(object sender, RoutedEventArgs e) {
			if (!String.IsNullOrEmpty(diseaseSource.GraphML)) {
				System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(GraphDrawer.GraphML.graphml));
				System.IO.StringReader reader = new System.IO.StringReader(diseaseSource.GraphML);
				var graph = (GraphDrawer.GraphML.graphml)ser.Deserialize(reader);

				foreach (var item in graph.graph.node) {
					SolidColorBrush c = new SolidColorBrush(Colors.Transparent);
					foreach (var color in item.data) {
						if (color.key == "nodeColor") {

							try {
								c = new SolidColorBrush(HexColor(color.Value));
							}
							catch (Exception) {
								try {
									c = new SolidColorBrush((Color)(typeof(Colors)).GetProperty(color.Value).GetValue(null, null));
								}
								catch (Exception) {


								}
							}

						}
					}
					c.Opacity = 0.5;
					statesColors.Add(item.id, c);
				}
			}
			EnableNext();
		}

		private void WizardMenu_SizeChanged(object sender, SizeChangedEventArgs e) {
			wizard.Height = ActualHeight;
			wizard.Width = ActualWidth;

			PopUp.Width = e.NewSize.Width;
			PopUp.Height = e.NewSize.Height;
		}

		private void WizardMenu_PageEvent(Wizard sender, WizardEventArgs e) {
			if (e.NewPageIndex == 0) // Entry
				EnableNext();
			else if (e.NewPageIndex == 1) // Network source
				DisableNext();
			else if (e.NewPageIndex == 2) { // Disease source
				DisableNext();
				statesColors.Clear();
			}
			else if (e.NewPageIndex == 3) { // Setup
				setup.Init();
				EnableNext();
			}
			else if (e.NewPageIndex == 4) // Simulation
				simulation.Init();
			else if (e.NewPageIndex == 5) { // Reports
				DisableNext();
				chart.Init();
			}
		}
	}
}
